﻿using System;
using System.Collections.Generic;
using System.Text;
using StudentManager.Core;
using System.Data;
using System.Data.SqlClient;
using StudentManager.Model;

namespace StudentManager.DataAccess
{
    class GiaoVienAccess
    {
        dbConnection db = new dbConnection();

        public GiaoVienAccess()
        {

        }
        public DataTable layToanbogiaovien()
        {
            return db.executeSelectProcedureQueryNonParameter("select_giaovien");
        }
        //---- Tìm kiếm theo mã giáo viên
        public DataView timkiemMagv(string MaGV)
        {
            DataView dataView = layToanbogiaovien().DefaultView;
            dataView.RowFilter = string.Format("MAGV like '%{0}%'", MaGV);
            return dataView;
        }
        //---- Tìm kiếm theo tên giáo viên
        public DataView timkiemTengv(string TenGv)
        {
            DataView dataView = layToanbogiaovien().DefaultView;
            dataView.RowFilter = string.Format("TENGV like '%{0}%'", TenGv);
            return dataView;
        }
        //---- Tim Giao vien theo ma va tra ve mot bang
        public DataTable timGiaovientheoma(string magv)
        {
            SqlParameter[] listParam= new SqlParameter[1];
            listParam[0]= new SqlParameter("MAGV",SqlDbType.NVarChar,10);
            listParam[0].Value = magv;
            return db.executeSelectProcedureQuery("select_giaovien_theoma", listParam);
        }
        //---- Tim Giao vien theo ma va tra ve mot doi tuong
        //public Giaovien timGiaovientheoma(string magv)
        //{
        //    //DataTable dataTable = timGiaovientheoma(magv);
            
        //}
        //---- Thêm giáo viên
        public int themGiaovien(Giaovien gv)
        {
            
            if (timGiaovientheoma(gv.MAGV1).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter magv = new SqlParameter("@MAGV", SqlDbType.VarChar, 10);
                SqlParameter malh = new SqlParameter("@MALH", SqlDbType.VarChar, 10);
                magv.Value = gv.MAGV1;
                malh.Value = gv.MALH1;
                listParam[0] = magv;
                listParam[1] = new SqlParameter("@TENGV", gv.TENGV1);
                listParam[2] = new SqlParameter("@GIOITINH", gv.GIOITINH1);
                listParam[3] = new SqlParameter("@NGAYSINH", string.Format("{0:yyyy-MM-dd}", gv.NGAYSINH1));
                listParam[4] = new SqlParameter("@SDT", gv.SDT1);
                listParam[5] = malh;
                db.executeInsertProcedureQuery("them_giaovien", listParam);
                return 1;
            }
        }
        //---- Sửa giáo viên
        public int suaGiaovien(Giaovien gv)
        {

            if (timGiaovientheoma(gv.MAGV1).Rows.Count < 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter magv = new SqlParameter("@MAGV", SqlDbType.VarChar, 10);
                SqlParameter malh = new SqlParameter("@MALH", SqlDbType.VarChar, 10);
                magv.Value = gv.MAGV1;
                malh.Value = gv.MALH1;
                listParam[0] = magv;
                listParam[1] = new SqlParameter("@TENGV", gv.TENGV1);
                listParam[2] = new SqlParameter("@GIOITINH", gv.GIOITINH1);
                listParam[3] = new SqlParameter("@NGAYSINH", string.Format("{0:yyyy-MM-dd}", gv.NGAYSINH1));
                listParam[4] = new SqlParameter("@SDT", gv.SDT1);
                listParam[5] = malh;
                db.executeUpdateProcedureQuery("sua_giaovien", listParam);
                return 1;
            }
        }
        //---- Xóa giáo viên
        public int xoaGiaovien(Giaovien gv)
        {

            if (timGiaovientheoma(gv.MAGV1)==null) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
                listParam[0] = new SqlParameter("@MAGV", gv.MAGV1);
                db.executeDeleteProcedureQuery("xoa_giaovien", listParam);
                return 1;
            }
        }
    }
}
